﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using SdmxMl.Helper;
using SdmxMl.Manager;
using System.IO;

namespace ActivityMgr.HGDE
{
    public partial class DlgHclCorr : Form
    {
        private BindingSource myBindingSource;
        private DateTime dtStart;
        private DateTime dtEnd;
        private HclCorrespondance _corresp;

        public DlgHclCorr()
        {
            InitializeComponent();
            dateTimePickerStart.Value = DateTime.Parse("1960.01.01");
        }
        private void buttonStartCorrespondances_Click(object sender, EventArgs e)
        {

            try
            {
                using (new CWaitCursor())
                {
                    // dates min max
                    dtStart = dateTimePickerStart.Value;
                    dtEnd = dateTimePickerEnd.Value;
                    if (dtStart > dtEnd)
                    {
                        dtStart = dateTimePickerEnd.Value;
                        dtEnd = dateTimePickerStart.Value;
                        dateTimePickerStart.Value = dtStart;
                        dateTimePickerEnd.Value = dtEnd; ;
                    }

                    // Reuse container if exists
                    SmManager sm = null;
                    if (_corresp != null)
                        sm = _corresp.SM;

                    _corresp = new HclCorrespondance("CH1_RN+HCL_HGDE_HIST+1.0", sm);
                    _corresp.PrepareTimeFrame(dtStart, dtEnd, checkBoxChangesOnly.Checked == false,
                        checkBoxAddText.Checked, checkBoxAddParent.Checked, checkBoxNonHistChanges.Checked);
                    DataTable tb = _corresp.CreateLevelTable(2, "corr");
                    DataSet ds = new DataSet();
                    ds.Tables.Add(tb);


                    myBindingSource = new BindingSource();
                    if (ds.Tables.Count > 0)
                    {
                        myBindingSource.DataSource = null;
                        myBindingSource.DataSource = ds;
                        myBindingSource.DataMember = ds.Tables[0].TableName;
                        textBoxInfo.Text = myBindingSource.Count.ToString();
                    }
                    else
                    {
                        myBindingSource.DataSource = null;
                        textBoxInfo.Text = "0";
                    }

                    dataGridViewCorr.DataSource = myBindingSource;
                }


                //tb.WriteXml("C:\\Table.xml");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

        }

        private void buttonLoadExcel_Click(object sender, EventArgs e)
        {
            BindingSource bs = dataGridViewCorr.DataSource as BindingSource;
            if (bs != null)
            {
                DataSet ds = myBindingSource.DataSource as DataSet;
                if (ds != null && ds.Tables.Count > 0)
                {
                    saveFileDialog.FileName = "HGDE_CORRESP_" +
                        dtStart.ToString("yyyy-MM-dd") + "_" +
                        dtEnd.ToString("yyyy-MM-dd") +
                        ".csv";

                    if (saveFileDialog.ShowDialog() == DialogResult.OK)
                        GenerateExcel(saveFileDialog.FileName, ds.Tables[0]);
                }
            }
        }

        private void GenerateExcel(string path, DataTable t)
        {
            string separ = ";";

            try
            {
                using (StreamWriter sw = new StreamWriter(path, false, Encoding.Default))
                {
                    // Header
                    foreach (DataColumn col in t.Columns)
                    {
                        sw.Write(col.ColumnName);
                        sw.Write(separ);
                    }
                    sw.WriteLine();

                    // Datas
                    foreach (DataRow row in t.Rows)
                    {
                        for (int i = 0; i < t.Columns.Count; i++)
                        {
                            sw.Write(row[i].ToString());
                            sw.Write(separ);
                        }

                        sw.WriteLine();
                    }

                    sw.Flush();
                    sw.Close();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void buttonCreateSdmx_Click(object sender, EventArgs e)
        {
            try
            {
                DlgOfsCorresp aDlg = new DlgOfsCorresp();
                aDlg.ShowDialog();

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }


        }
    }
}
        
